﻿using NeuroCheck.BaseTypes.DataFormatConverter;
using NeuroCheck.Driver.UIServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CLVisionCommuDFC.UI
{

    public class FmtCnvUI : INcFmtCnvDriver, IDisposable
    {
        private static volatile FmtCnvUI m_Instance = null;
        private static INcFmtCnvDriver m_FmtCnvCore = null;
        public static FmtCnvUI CreateInstance()
        {
            lock (typeof(FmtCnvUI))
            {
                if (null == m_Instance)
                {
                    try
                    {
                        m_Instance = new FmtCnvUI();
                        FmtCnvCoreFactory fmtCnvCoreFactory = new FmtCnvCoreFactory();
                        m_FmtCnvCore = fmtCnvCoreFactory.CreateInstance();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return m_Instance;
        }

        public bool ControlChannel(int channelID, NcFmtCnvControlChannelCodeInfo selCode, ref object data, ref object dataEx)
        {
            switch (selCode)
            {
                default:
                    {
                        return m_FmtCnvCore.ControlChannel(channelID, selCode, ref data, ref dataEx);
                    }
            }
        }

        public bool ControlDriver(NcFmtCnvControlDriverCodeInfo selCode, ref object data, ref object dataEx)
        {
            switch (selCode)
            {
                case NcFmtCnvControlDriverCodeInfo.GetProductLogo:
                    data = Properties.Resource1.MODBUSNC;
                    break;
                case NcFmtCnvControlDriverCodeInfo.GetCompanyLogo:
                    data = Properties.Resource1.CLlogo;
                    break;
                case NcFmtCnvControlDriverCodeInfo.ShowAboutDialog:
                    using (AboutDialog dlg = new AboutDialog())
                    {
                        dlg.ShowDialog();
                    }
                    break;
                case NcFmtCnvControlDriverCodeInfo.CheckLicense:
                    break;
                default:
                    return m_FmtCnvCore.ControlDriver(selCode, ref data, ref dataEx);
                  
            }
            return true;
        }

        public bool ControlPin(int channelID, int pinIDOrIndex, NcFmtCnvControlPinCodeInfo selCode, ref object data, ref object dataEx)
        {
            return m_FmtCnvCore.ControlPin(channelID, pinIDOrIndex, selCode, ref data, ref dataEx);
        }

        public bool CreateChannel(NcFmtCnvIOCapsInfo caps, ref object deviceDescription, out int channelID)
        {
            return m_FmtCnvCore.CreateChannel(caps, ref deviceDescription, out channelID);
        }

        public bool DeleteChannel(int channelID)
        {
            return m_FmtCnvCore.DeleteChannel(channelID);
        }

        public void Dispose()
        {
            ((IDisposable)m_FmtCnvCore).Dispose();

        }

        public bool GetChannelCount(out int channelCount)
        {
            return m_FmtCnvCore.GetChannelCount(out channelCount);
        }

        public bool GetChannelDisplayName(int channelID, out string channelDisplayName)
        {
            return m_FmtCnvCore.GetChannelDisplayName(channelID, out channelDisplayName);
        }

        public bool GetChannelID(int channelIndex, out int channelID)
        {
            return m_FmtCnvCore.GetChannelID(channelIndex, out channelID);
        }

        public bool GetFmtCnvInfo(out string name, out string description)
        {
            return m_FmtCnvCore.GetFmtCnvInfo(out name, out description);
        }

        public bool GetFmtCnvType(out NcFmtCnvTypeInfo type)
        {
            return m_FmtCnvCore.GetFmtCnvType(out type);
        }

        public NcFmtCnvErrorObject GetLastError()
        {
            return m_FmtCnvCore.GetLastError();
        }

        public bool GetPinCount(int channelID, out int pinCount)
        {
            return m_FmtCnvCore.GetPinCount(channelID, out pinCount);
        }
    }
}
